<template>
  <div>
    <FormPanel ref="form" :list="formList" @formChange="changeHandle" :isSubmitBtn="false" :collapse="false" labelWidth="110" />
    <BreakSpace label="合格证信息" />
    <FilterTable ref="table" :isSelectColumn="false" :columns="columns" :dataSource="tableList" :onColumnsChange="columns => (this.columns = columns)"> </FilterTable>

    <div
      :style="{
        position: 'absolute',
        left: 0,
        bottom: 0,
        right: 0,
        zIndex: 9,
        borderTop: '1px solid #e9e9e9',
        padding: '10px 20px',
        background: '#fff',
        textAlign: 'center'
      }"
    >
      <multiuse-button type="primary" :click="dataHandle.bind(this, 60651002)">邮寄确认</multiuse-button>
      <multiuse-button type="primary" :click="dataHandle.bind(this, 60651001)">保 存</multiuse-button>
      <el-button @click="closeDrawer">关 闭</el-button>
    </div>
  </div>
</template>
<script>
import moment from 'moment';
import { dictionary } from '@/utils/dictMixin';
import { getUser } from '@/assets/js/auth';
import { queryInfoById, saveData, gettmFiAccountType } from '@/api/omSale/certificate/MailRegistration';

export default {
  mixins: [dictionary],
  props: ['type', 'rowData'],

  data() {
    this.BaseTable = null;
    this.saveData = null;
    return {
      formList: this.createdFrom(),
      columns: this.createdTable(),
      tableList: [],
      saveList: [],
      postId: null
    };
  },
  created() {
    this.init();
    this.gettmFiAccountType();
  },
  mounted() {
    this.BaseTable = this.$refs.table.$refs.pageTable;
  },
  methods: {
    async init() {
      console.log(this.rowData, '---奥利给---');
      const res = await queryInfoById(this.rowData);
      this.tableList = [];

      if (res.resultCode == 200) {
        this.tableList = res.data.salesOrderlist;
        this.postId = res.data.postId;
        this.formList.map(x => {
          if (x.fieldName == 'postPerson') {
            x.initialValue = res.data[x.fieldName] ? res.data[x.fieldName] : getUser();
          } else if (x.fieldName == 'postDate') {
            x.initialValue = res.data[x.fieldName] ? res.data[x.fieldName] : moment().format('YYYY-MM-DD hh:mm:ss');
          } else {
            x.initialValue = res.data[x.fieldName];
          }
        });
      }
    },
    // 加载资金类型
    async gettmFiAccountType() {
      let res = (await gettmFiAccountType({ fundsType: 0 })).data.map(x => ({ text: x.accountTypeName, value: `${x.accountTypeCode}` }));
      this.columns.find(item => item.dataIndex === 'accountTypeCode').editItems = res; //操作姓名
      this.columns.find(item => item.dataIndex === 'accountTypeCode').filterItems = res;
    },
    changeHandle(val) {
      this.saveData = val;
    },
    delHandle(row) {
      this.BaseTable.EXECUTE_DELETE([row]);
    },

    async dataHandle(status) {
      this.$refs.form.SUBMIT_FORM();
      if (this.saveData) {
        if (this.tableList.length == 0) return this.$message.error('暂无合格证信息数据！');

        this.saveData.postId = this.postId ? this.postId : '';
        this.saveData.postStatus = status;
        this.saveData.salesOrderlist = this.tableList;
        let res = await saveData(this.saveData);

        if (res.resultCode == 200) {
          this.$notify.success({ title: '提示信息', message: '操作成功' });
          this.$emit('close', false, 'refresh');
        }
      }
    },
    closeDrawer() {
      this.$emit('close', false);
    },
    createdFrom() {
      return [
        {
          type: 'BREAK_SPACE',
          label: '邮寄信息'
        },
        {
          type: 'INPUT',
          label: '经销商代码',
          fieldName: 'dealerCode',
          disabled: true,
          placeholder: '请输入'
        },
        {
          type: 'INPUT',
          label: '经销商名称',
          fieldName: 'dealerName',
          disabled: true,
          placeholder: '请输入'
        },
        {
          type: 'INPUT',
          label: '金融机构',
          fieldName: 'bankName',
          disabled: true,
          placeholder: '请输入'
        },
        {
          type: 'INPUT',
          label: '邮寄人',
          fieldName: 'postPerson',
          initialValue: getUser(),
          disabled: true,
          placeholder: ''
        },

        {
          type: 'INPUT',
          label: '收件人',
          fieldName: 'receivePerson',
          disabled: true,
          placeholder: '请输入'
        },
        {
          type: 'INPUT',
          label: '收件人电话',
          fieldName: 'receivePhone',
          disabled: true,
          placeholder: '请输入'
        },
        {
          type: 'INPUT',
          label: '收件地址',
          fieldName: 'receiveAddress',
          disabled: true,
          selfCols: 2,
          placeholder: '请输入'
        },
        {
          type: 'INPUT',
          label: '快递单号',
          fieldName: 'expressNumber',
          placeholder: '请输入',
          rules: [{ required: true, message: '请输入', trigger: 'blur' }]
        },
        {
          type: 'INPUT',
          label: '快递公司',
          fieldName: 'expressCompany',
          placeholder: '请输入',
          rules: [{ required: true, message: '请输入', trigger: 'blur' }]
        },
        {
          type: 'DATE',
          label: '快递时间',
          fieldName: 'postDate',
          dateType: 'datetime',
          initialValue: moment().format('YYYY-MM-DD hh:mm:ss'),
          rules: [{ required: true, message: '请选择', trigger: 'change' }]
        }
      ];
    },
    createdTable() {
      return [
        {
          title: '序号',
          dataIndex: 'index',
          sorter: true,
          fixed: 'left',
          render: props => {
            return <span>{props.row.index + 1}</span>;
          }
        },
        {
          title: '操作',
          dataIndex: 'column-action',
          fixed: 'left',
          render: (props, h) => {
            return (
              <div>
                <multiuse-button size="mini" type="text" onClick={() => this.delHandle(props.row)}>
                  删除
                </multiuse-button>
              </div>
            );
          }
        },
        {
          title: '销售订单号',
          dataIndex: 'doNo',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: 'VIN',
          dataIndex: 'vin',
          align: 'center',
          width: 180,
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '合格证号',
          dataIndex: 'certificateNo',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '资金类型',
          dataIndex: 'accountTypeCode',
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: [],
          editType: 'select',
          editItems: []
        },
        {
          title: '三方票号',
          dataIndex: 'billNo',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '产品名称',
          dataIndex: 'productName',
          sorter: true,
          filter: true,
          filterType: 'input'
        }
      ];
    }
  }
};
</script>
